define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data('area',["100%",'100%']);
            $(".btn-edit").data('area',["100%",'100%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sales/report/groupranking/index' + location.search,
                    table: 'sales_slip',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                dblClickToEdit: false,
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class: 'sid',operate:false},
                        // {field: 'createtime', title: __('时间')},
                        {field: 'name', title: __('部门编号'), formatter: Controller.api.formatter.number,operate:false},
                        {field: 'name', title: __('部门名称'),operate:false},
                        // {field: 'name', title: __('浮动单位数量'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'sale_num', title: __('销售数量'),operate:false},
                        {field: 'sale_money', title: __('单价'),operate:false},
                        {field: 'sale_price', title: __('销售金额'),operate:false},
                        {field: 'actual_cost', title: __('价税合计'),operate:false},
                        {field: 'tax_price', title: __('税额'),operate:false},
                        {field: 'onecost', title: __('成本单价'),operate:false},
                        {field: 'cost', title: __('成本金额'),operate:false},
                        {field: 'profit', title: __('毛利'),operate:false},
                        {field: 'profit_margin', title: __('毛利率'),operate:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [

                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true)
                                // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                // console.log(row)
                                if(row['ystatus'] != 1){
                                    $(table).data("operate-edit", null);// 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }}
                    ]
                ]
            });

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#num").text(data.num);
                $("#price").text(data.price);
            });
            $('table').on('dblclick','td',function(){

            });
            table.on('post-body.bs.table',function () {
                $(".btn-editone").data('area',["100%",'100%']);
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-add").click(function () {

                if($('#c-storehouse_id').val()== 0 ){
                    layer.msg('未选中出库仓库')
                    return false;
                }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: 'seach?house_id='+$('#c-storehouse_id').val(),
                })
            })
            var customer_id = 0;
            $(document).on("change", "#c-customer_id", function () {
                var cid = $('#c-customer_id').val()
                if(cid == ''){
                    return  false;
                }
                if(cid == customer_id){
                    return false;
                }
                customer_id = cid;
                var url = Config.this_url+'/customer/customer/detail?ids='+cid
                $.post(url,{},function (res) {
                    $('#default_mobile').val(res.mobile)
                    $('#default_address').val(res.address)
                    $('#default_contacts').val(res.nickname)
                })
            })
            Controller.api.bindevent();
        },
        edit: function () {
            // $(".btn-add").click(function () {
            //     layer.open({
            //         type: 2, // 1-在content中写html代码， 2-在content中写src路径
            //         area: ['70%', '90%'],
            //         title: "选择商品",
            //         content: Config.this_url+'/sales/slip/seach',
            //     })
            // })
            $(".btn-add").click(function () {

                if($('#c-storehouse_id').val()== 0 ){
                    layer.msg('未选中出库仓库')
                    return false;
                }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: Config.this_url+'/sales/slip/seach?house_id='+$('#c-storehouse_id').val(),
                })
            })
            var customer_id = 0;
            $(document).on("change", "#c-customer_id", function () {
                var cid = $('#c-customer_id').val()
                if(cid == ''){
                    return  false;
                }
                if(cid == customer_id){
                    return false;
                }
                customer_id = cid;
                var url = Config.this_url+'/customer/customer/detail?ids='+cid
                $.post(url,{},function (res) {
                    $('#default_mobile').val(res.mobile)
                    $('#default_address').val(res.address)
                    $('#default_contacts').val(res.nickname)
                })
            })
            Controller.api.bindevent();
        },
        detail:function(){
            $('.btn-end').click(function () {
                    var ids = $(this).data('id');
                    layer.confirm('是否中止该商品？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        var url = Config.this_url +'/sales/slip/stop?ids='+ids
                        $.post(url,{},function (res) {
                            layer.msg(res.msg)
                            if(res.code == 1){
                                setInterval(function () {
                                    window.location.reload()
                                },1000)
                            }
                        })
                    })
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                number: function (value, row, index) {
                    //这里手动构造URL
                    url = "discuss?" + this.field + "=" + value;
                    url = "sales/report/checkoutgoods?company=" + value+'&createtime='+$('.datetimerange').val();

                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("Search %s", value) + '">' + __( value) + '</a>';

                    //方式二,直接调用Table.api.formatter.addtabs
                    return Table.api.formatter.addtabs(value, row, index, url);
                }
            }
        },
        seach:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/specs',
                    table: '',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize:15,
                // searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        {field: 'title', title: __('title'),  operate: false},
                        {field: 'place_origin', title: __('place_origin'), formatter: Table.api.formatter.search},
                        {field: 'number', title: __('number')},
                        {field: 'unit', title: __('unit'), align: 'left'},
                        {field: 'specs', title: __('specs')},
                        {field: 'stock', title: __('stock')},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(sid);
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){

                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);

            });

            // $(document).scroll(function() {
            //     if ($(document).height()-$(document).scrollTop()-window.innerHeight == 0) {
            //         console.log()
            //         if($('.pagination').find('.active').parents('div').css('display') == 'none'){
            //
            //         }else{
            //             setTimeout(function () {
            //                 $('.pagination').find('.active').next().click()
            //                 $('html,body').animate({scrollTop:0},1000);//回到顶端
            //             },500)
            //         }
            //
            //
            //         // console.log(1)
            //     } else {
            //         // console.log(2)
            //     }
            // })
            // $(window).scroll(function ()
            // {
            //     var st = $(this).scrollTop();
                // console.log(st)
                // if(st == 0){
                //     if($('.pagination').find('.active').parents('div').css('display') == 'none'){
                //
                //     }else{
                //         setTimeout(function () {
                //             $('.pagination').find('.active').prev().click()
                //             $('html,body').animate({scrollTop:0},1000);//回到顶端
                //         },500)
                //     }
                // }
            // });
            $('div.mousewheel_example')
                .bind('mousewheel', function(event, delta) {
                    var dir = delta > 0 ? 'Up' : 'Down',
                        vel = Math.abs(delta);
                    $(this).text(dir + ' at a velocity of ' + vel);
                    return false;
                });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

    };
    return Controller;
});